package com.weipu.common.facade.impl;

import android.content.Context;

import com.weipu.common.constants.Constant;
import com.weipu.common.facade.UpateServerProvider;
import com.weipu.common.facade.model.UpdateModel;
import com.weipu.common.okhttp.OkHttpHelper;
import com.weipu.common.util.Common;
import com.weipu.common.util.Logger;
import com.weipu.common.util.utils.SPUtils;
import com.weipu.dx_lib.R;

/**
 * APP更新服务实现类
 *
 * @author Zenglw
 * @version [1.0, 2017-6-15]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */

public class UpdateServiceProviderImpl extends BaseServiceProviderImpl implements UpateServerProvider {

    /**
     * 从服务器上获取最新的版本号
     *
     * @return
     */
    public String getVersion(Context context) {

        OkHttpHelper okHttpHelper = OkHttpHelper.getOkHttpHelperInstance();
        UpdateModel updateModel = okHttpHelper.syncGetRequestByModel(Constant.APP_GET_VERSION_URL, UpdateModel.class);
        if (updateModel != null
                && updateModel.getData() != null
                && updateModel.getData().getAndroid() != null
                && updateModel.getData().getAndroid().getVersionCode() != null) {
            UpdateModel.DataBean.AndroidBean android = updateModel.getData().getAndroid();
            SPUtils spUtils = new SPUtils(context.getResources().getString(R.string.app_name_en));
            //保存服务器的版本号
            spUtils.put(Constant.Global.APP_VERSION_CODE, android.getVersionCode());
            if (android.getUpdateContent() != null) {

                //保存更新内容
                spUtils.put(Constant.Global.APP_UPDATE_CONTENT, android.getUpdateContent().isEmpty() ?
                                context.getResources().getString(R.string.app_version_update_content) : android.getUpdateContent());
            }else {
                //保存更新内容
                spUtils.put(Constant.Global.APP_UPDATE_CONTENT, context.getResources().getString(R.string.app_version_update_content));
            }
            return android.getVersionCode();
        }
        return String.valueOf(Common.getVersionName());
    }

    @Override
    public boolean checkUpdateCancelable(Context context) {

        if (!String.valueOf(Common.getVersionName()).equals(getVersion(context))) {
            Logger.e("version " + Common.getVersionName());
            return true;
        }

        return false;
    }


    /**
     * 开始下载apk
     *
     * @param context
     */
    @Override
    public void startDownloadApk(Context context) {

//        String url = "https://storage.evozi.com/apk/dl/16/10/11/com.epro.dx.apk?h=FL4-udbBEE94J_ctBo90KA&t=1497324209";
//        DownloadManager downloadManager = (DownloadManager) DXApplication.getContext().getSystemService(Context.DOWNLOAD_SERVICE);
//        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
//        //如果不存在改目录，则创建这个目录
//        Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).mkdir();
//        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "dx.apk");
//        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
//        request.setTitle("dx.com文件");
//        request.setDescription("正在下载中......");
//        request.setNotificationVisibility(VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
//        request.setMimeType("application/vnd.android.package-archive");
//        downloadManager.enqueue(request);


    }
}
